import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/homeworkDetailDatagrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:intl/intl.dart';

// ignore: must_be_immutable
class HomeworkProgressPerStudent extends StatefulWidget {
  HomeworkProgressPerStudent(
      {Key? key, required this.assignmentId, required this.userName})
      : super(key: key);
  static const String routeName = '/teacher/reports/homeworkprogress/student';
  String assignmentId;
  String userName;

  @override
  _HomeworkProgressPerStudent createState() => _HomeworkProgressPerStudent();
}

class _HomeworkProgressPerStudent extends State<HomeworkProgressPerStudent> {
  final ReportsController _reportsController = Get.find();
  var box = Hive.box('appconfig');

  @override
  void initState() {
    var classId = box.get(CacheManagerKey['currentclass'].toString());
    Future.delayed(Duration(milliseconds: 300), () {
      _reportsController.fetchAssignmentStudentReport(
          widget.assignmentId, widget.userName);
    });
    Future.delayed(Duration(milliseconds: 600), () {
      _reportsController.fetchAssignmentReportForUser(
          classId, widget.assignmentId, widget.userName);
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher assignment progress per student');

    return Scaffold(
      appBar: CustomAppBar(title: widget.userName),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            children: [
              _reportsController.obx((data) {
                var student =
                    _reportsController.assignmentStudentData.value.student;
                if (student != null) {
                  return Padding(
                      padding: const EdgeInsets.all(20.0),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: 80,
                            height: 80,
                            child: student.profileImageUrl != null
                                ? Image.network(
                                    student.profileImageUrl!,
                                  )
                                : Image.asset(
                                    'assets/images/profileAvartar.png',
                                  ),
                          ),
                          SizedBox(
                            width: 30,
                          ),
                          Expanded(
                              child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: [
                              Text(
                                student.firstName! + ' ' + student.lastName!,
                                style: Theme.of(context).textTheme.titleMedium,
                              ),
                              Row(
                                children: [
                                  Text(
                                    "Username:".tr,
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    student.userName!,
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  )
                                ],
                              ),
                              Row(
                                children: [
                                  Text(
                                    "Points Earned:".tr,
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    student.points!.toString(),
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  )
                                ],
                              ),
                              Row(
                                children: [
                                  Text(
                                    "Last Login:".tr,
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    student.lastLoggedIn! > 0
                                        ? DateFormat('MM/dd/yyyy kk:mm').format(
                                            DateTime.fromMillisecondsSinceEpoch(
                                                student.lastLoggedIn!))
                                        : '--',
                                    style:
                                        Theme.of(context).textTheme.titleSmall,
                                  )
                                ],
                              )
                            ],
                          ))
                        ],
                      ));
                } else {
                  return Text('');
                }
              }),
              _reportsController.obx((data) {
                return Container(
                  padding: EdgeInsets.all(
                    30.0,
                  ),
                  decoration: BoxDecoration(
                      color: Get.isDarkMode
                          ? Colors.black
                          : Colors.white.withOpacity(0.6),
                      borderRadius: BorderRadius.circular(3.0)),
                  child: Column(children: [
                    Row(
                      children: [
                        Flexible(
                          child: Row(children: [
                            Text(
                              "${_reportsController.assignmentStudentData.value.workType} status: ",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            Text(
                              _reportsController.assignmentStudentData.value
                                          .startDate !=
                                      null
                                  ? (_reportsController.assignmentStudentData
                                              .value.startDate! >
                                          DateTime.now().millisecondsSinceEpoch
                                      ? "Queued"
                                      : _reportsController.assignmentStudentData
                                                  .value.endDate! <
                                              DateTime.now()
                                                  .millisecondsSinceEpoch
                                          ? "Closed"
                                          : "Open")
                                  : "loading",
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ]),
                          flex: 1,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Flexible(
                            child: Row(
                          children: [
                            Text(
                              "Create Date: ",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              _reportsController.assignmentStudentData.value
                                          .creatDate !=
                                      null
                                  ? DateFormat('yyyy-MM-dd kk:mm').format(
                                      DateTime.fromMillisecondsSinceEpoch(
                                          _reportsController
                                              .assignmentStudentData
                                              .value
                                              .creatDate!))
                                  : "",
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ],
                        ))
                      ],
                    ),
                    Row(
                      children: [
                        Flexible(
                          child: Row(children: [
                            Text(
                              "Progress(Book)",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              _reportsController.assignmentStudentData.value
                                          .overallComplete !=
                                      null
                                  ? "Completed-${_reportsController.assignmentStudentData.value.overallComplete}, Total-${_reportsController.assignmentStudentData.value.totalBooks}, Required-${_reportsController.assignmentStudentData.value.requiredBooks}"
                                  : '',
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ]),
                          flex: 1,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Flexible(
                            child: Row(
                          children: [
                            Text(
                              "Start Date:",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              _reportsController.assignmentStudentData.value
                                          .creatDate !=
                                      null
                                  ? DateFormat('yyyy-MM-dd kk:mm').format(
                                      DateTime.fromMillisecondsSinceEpoch(
                                          _reportsController
                                              .assignmentStudentData
                                              .value
                                              .creatDate!))
                                  : "",
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ],
                        ))
                      ],
                    ),
                    Row(
                      children: [
                        Flexible(
                          child: Row(children: [
                            Text(
                              "",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              "",
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ]),
                          flex: 1,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Flexible(
                            child: Row(
                          children: [
                            Text(
                              "Due Date: ",
                              style: Theme.of(context).textTheme.titleSmall,
                            ),
                            Text(
                              _reportsController.assignmentStudentData.value
                                          .endDate !=
                                      null
                                  ? DateFormat('yyyy-MM-dd kk:mm').format(
                                      DateTime.fromMillisecondsSinceEpoch(
                                          _reportsController
                                              .assignmentStudentData
                                              .value
                                              .endDate!))
                                  : "",
                              style: Theme.of(context).textTheme.bodySmall,
                            )
                          ],
                        ))
                      ],
                    )
                  ]),
                );
              }),
              Expanded(
                child: _reportsController.obx(
                  (data) {
                    return HomeworkDetailDataGrid(
                        _reportsController.assignmentStudentDetailList,
                        _reportsController
                                    .assignmentStudentData.value.endDate !=
                                null
                            ? (_reportsController
                                    .assignmentStudentData.value.endDate! <
                                DateTime.now().millisecondsSinceEpoch)
                            : false);
                  },
                  onEmpty: Text('Data not available ...'.tr),
                  onLoading: Container(),
                  // onLoading: Center(
                  //     child: CircularProgressIndicator(
                  //   backgroundColor: Get.theme.primaryColorDark,
                  //   strokeWidth: 9.0,
                  // )),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
